package com.s4j.core.manager.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.s4j.core.common.StringUtils;
import com.s4j.core.conset.Constant;
import com.s4j.core.datagrid.DatagridPaging;
import com.s4j.core.hibernate.CommonDao;
import com.s4j.core.manager.model.SysProject;
import com.s4j.core.manager.model.SysUser;
import com.s4j.core.manager.model.SysUserAuth;

@Service
public class SysUserService {
	
//	private static Logger log = Logger.getLogger(ManagerAction.class);
	
	@Resource
	private CommonDao dao;

	public DatagridPaging findAllUser() {
		String hql = "from SysUser order by userId asc";
		DatagridPaging users = dao.findByHQLPaging(hql, 1, 10);
		return users;
	}

	public Long insert(SysUser user) {
		return (Long)dao.save(user);
	}

	public DatagridPaging findUserByPaging(Integer page, Integer rows, SysUser user) {
		List params = new ArrayList();
		String sql = "select user.userId as userId, " +
				" user.userName as userName, " +
				" person.personName as personName " +
				" from SysUser user left join SysPerson person " +
				" on user.person.personId = person.personId " +
				" where 1 = 1 ";
		if(user != null && !StringUtils.isNullOrBlank(user.getUserName())) {
			sql += " and user.userName like ?";
			params.add('%' + user.getUserName() + '%');
		}
		sql +=  " order by user.userId asc";
		params.add(page);
		params.add(rows);
		DatagridPaging users = dao.findByHqlPagingFormatMap(sql, params.toArray());
		return users;
	}
	
	public SysUser getUser(Long userId) {
		return dao.get(SysUser.class, userId);
	}

	public void update(SysUser user) {
		dao.update(user);
	}

	public void remove(String userIds) {
		String[] idArr = userIds.split(",");
		for(int i=0; i<idArr.length; i++) {
			String userId = idArr[i];
			if(StringUtils.isNullOrBlank(userId)) {
				continue;
			}
			dao.remove(SysUser.class, new Long(userId));
		}
	}

	public List<Map<String, Object>> authTree(Long userId, Long parentId) {
		
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		
		String hql = "from SysProject p where p.parent.projectId = ? order by p.sortNum asc";
		
		List<SysProject> pros = dao.findByHQL(hql, SysProject.class, parentId);
		
		for(int i=0; i<pros.size(); i++) {
			
			Map<String, Object> map = new HashMap<String, Object>();
			
			SysProject project = pros.get(i);
			
			map.put("id", project.getProjectId());
			
			list.add(map);
			
			if(project.getChildren().size() > 0) {
				map.put("text", project.getProjectName());
				map.put("state", "open");
				map.put("children", authTree(userId, project.getProjectId()));
			} else {
				map.put("text", project.getProjectName());
				map.put("state", "open");
				map.put("checked", isAuth(userId, project.getProjectId()));
			}
		}
		
		return list;
	}

	public boolean isAuth(Long userId, Long projectId) {
		String sql = "select id from s4j_sysuserauth where userId = ? and resId = ? and resType = ?";
		List rtn = (List)dao.queryBySql(sql, userId, projectId, Constant.SYS_USER_AUTH_RESTYPE_PROJECT);
		if(rtn.size() > 0) {
			return true;
		}
		return false;
	}

	public void saveAuth(SysUser user, String resIds) {
		String sql = "delete from s4j_sysuserauth where userId = ?";
		dao.executeSql(sql, user.getUserId());
		String[] resIdArr = resIds.split(",");
		for(int i=0; i<resIdArr.length; i++) {
			if(!StringUtils.isNullOrBlank(resIdArr[i])) {
				Long resId = new Long(resIdArr[i]);
				SysUserAuth userAuth = new SysUserAuth();
				userAuth.setUserId(user.getUserId());
				userAuth.setResId(resId);
				userAuth.setResType(Constant.SYS_USER_AUTH_RESTYPE_PROJECT);
				dao.save(userAuth);
			}
		}
		
	}
}
